const shop = require('../../providers/dataAccess').shop;
const featureService = require('./featureService');
const skuService = require('./skuService');

const ProductService = function () {
   const _productList = async (appId,categoryId,brandId,keyName) => {
      var szWhere = "";
      if (categoryId != "") {
        szWhere = " AND categoryId in ("+ categoryId +")";
      }
      if (brandId != "") {
        szWhere += " AND brandId in (" + brandId + ")";
      }
      if (keyName != "") {
        szWhere += " AND productName like '%" + keyName + "%'";
      }

      var szSql = "SELECT id,salesCount,totalNumber,minAmount,maxAmount,thumbnail,unit,productName,description FROM products WHERE isOnline = 1 AND appId = ? " + szWhere + " ORDER BY addTime DESC";
      console.log(szSql);
      var rows = await shop.query(szSql,[appId]);
      return rows;
   };

   const _productInfo = async (appId,productId,userId) => {
      let szSql = "SELECT p.id,salesCount,totalNumber,minAmount,maxAmount,thumbnail,unit,productName,description,contentText FROM products AS p LEFT JOIN product_details AS d ON p.id = d.productId  WHERE p.id = ? AND isOnline = 1 AND p.appId = ? ";
      let rows = await shop.query(szSql,[productId,appId]);
      if (rows && rows.length > 0) {
         let info = rows[0];
         szSql = "SELECT imgUrl FROM product_imgs WHERE productId = ?";
         let imgs = await shop.query(szSql,[info.id]);
         info.features = await featureService.featureValues(info.id);
         info.featureItems = await featureService.featureItems(info.id);
         info.skus = await skuService.list(appId,info.id);
         info.imgs = imgs;
         info.isFavorite = false;
         szSql = "SELECT * FROM user_favorites WHERE appId = ? AND userId = ? AND productId = ?";
         let favorites = await shop.query(szSql,[appId,userId,info.id]);
         console.log(favorites);
         if (favorites && favorites.length) {
           info.isFavorite = true;
         }
         return info;
      } else {
        return null;
      }
   };

   const _addFavorite = async (appId,userId,productId,skuId) => {
     let szQuery = "REPLACE INTO user_favorites(appId,userId,productId,skuId) VALUES (?,?,?,?)";
     let ret = await shop.query(szQuery,[appId,userId,productId,skuId]);
     return ret.affectedRows > 0 ? true : false;
   };

   const _delFavorite = async (appId,userId,productId,skuId) => {
     let szQuery = "DELETE FROM user_favorites WHERE appId = ? AND userId = ? AND productId = ? AND skuId = ?";
     let ret = await shop.query(szQuery,[appId,userId,productId,skuId]);
     return ret.affectedRows > 0 ? true : false;
   };

   const _getFavorite = async (appId,userId,offset,limit) => {
     let szQuery = "SELECT P.* FROM user_favorites AS uf LEFT JOIN products AS P ON uf.productId = P.id WHERE uf.appId = ? AND uf.userId = ? ORDER BY addTime LIMIT ?,?";
     let rows = await shop.query(szQuery,[appId,userId,offset,limit]);
     /*if (rows && rows.length) {
       for (var i in rows) {
         let product = rows[i];
         szQuery = "SELECT saleAmount FROM product_skus WHERE id = ?";
         let list = await shop.query(szQuery,[product.skuId]);
         if (list && list.length) {
           product.saleAmount = list[i].saleAmount;
         }
       }
     }*/
     return rows && rows.length ? rows : [];
   };

   return {
       list: _productList,
       info: _productInfo,
       addFavorite: _addFavorite,
       getFavorite: _getFavorite,
       delFavorite: _delFavorite
   }
}();

module.exports = ProductService;
